<template>
<section>
  <el-row>
    <el-col :span="6">
      <el-input placeholder="请输入内容" v-model="searchKey" prefix-icon="el-icon-fa-search">
        <div slot="append">
          <el-button type="primary" icon="el-icon-fa-search" @click=" pager.pager.pageNumber = 1 ;doSearch()"></el-button>
        </div>
      </el-input>
    </el-col>
    <el-col :span="6" :offset="12">
      <el-button type="primary" icon="el-icon-fa-plus" @click="addmaterial" size="small">添加原料信息</el-button>
    </el-col>
  </el-row>
  <el-table :data="pager.dataList" border stripe style="width: 100%">
    <el-table-column prop="id" label="ID" header-align="center" align="center" width="55">
    </el-table-column>
    <el-table-column prop="name" label="品种" show-overflow-tooltip header-align="center" align="center">
    </el-table-column>
    <el-table-column prop="manor.name" label="庄园" show-overflow-tooltip header-align="center" align="center">
    </el-table-column>
    <el-table-column prop="storage.name" label="仓库" show-overflow-tooltip header-align="center" align="center">
    </el-table-column>
    <el-table-column prop="transport.arrivalTime" label="抵达时间" show-overflow-tooltip header-align="center" align="center">
    </el-table-column>
    <el-table-column label="操作" show-overflow-tooltip header-align="center" align="center">
      <template slot-scope="scope">
        <el-button-group>
          <el-button title="编辑原料信息" size="mini" type="primary" icon="el-icon-fa-edit" @click="handleEdit(scope.$index,scope.row)"></el-button>
          <el-button title="删除" size="mini" type="primary" icon="el-icon-fa-trash" @click="handleDelete(scope.$index,scope.row)"></el-button>
       </el-button-group>
      </template>
    </el-table-column>
  </el-table>
  <!--分页条 -->
  <el-row>
    <el-col :span="6" :offset="18">
      <el-pagination background small style="float:right" layout="prev, pager, next" :total="pager.pager.recordCount" :page-size="pager.pager.pageSize" :current-page.sync="pager.pager.pageNumber" v-show="pager.pager.pageCount != 0" @current-change="changePage">
      </el-pagination>
    </el-col>
  </el-row>
  <!-- 弹框区域-->
  <el-dialog :title="material.id == 0 ? '添加原料信息' : '编辑原料信息' " :visible.sync="addEditShow" width="50%">
    <el-form :model="material" :rules="$rules.material" ref="materialForm">
      <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
        <el-input v-model="material.name" auto-complete="off" suffix-icon="el-icon-fa-vcard" placeholder="请填写原料名称"></el-input>
      </el-form-item>
      <div class="order">
        <span style="white-space:pre;">  </span><span class="line"></span>
        <span style="white-space:pre;">  </span><span class="txt">品控信息</span>
        <span style="white-space:pre;">  </span><span class="line"></span>
      </div>
      <el-form-item label="瑕疵率" :label-width="formLabelWidth" prop="defectiveRate">
        <el-input v-model="material.defectiveRate" auto-complete="off"  placeholder="请填写原料瑕疵率">
          <template slot="append">％</template>
        </el-input>
      </el-form-item>
      <el-form-item label="含水率" :label-width="formLabelWidth" prop="rateOfWaterContent">
        <el-input v-model="material.rateOfWaterContent" auto-complete="off"  placeholder="请填写原料含水率">
          <template slot="append">％</template>
        </el-input>
      </el-form-item>
      <el-form-item label="密度" :label-width="formLabelWidth" prop="density">
        <el-input v-model="material.density" auto-complete="off"  placeholder="请填写原料密度">
          <template slot="append">g/L</template>
        </el-input>
      </el-form-item>
      <div class="order">
        <span style="white-space:pre;">  </span><span class="line"></span>
        <span style="white-space:pre;">  </span><span class="txt">关联信息</span>
        <span style="white-space:pre;">  </span><span class="line"></span>
      </div>
      <el-form-item label="庄园" :label-width="formLabelWidth" prop="manorId">
        <el-select v-model="material.manorId" placeholder="请选择庄园">
          <el-option v-for="item in manors" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="仓库" :label-width="formLabelWidth" prop="storageId">
        <el-select v-model="material.storageId" placeholder="请选择仓库">
          <el-option v-for="item in storages" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="运输信息" :label-width="formLabelWidth" prop="transportId">
        <el-select v-model="material.transportId" filterable  placeholder="请选择运输信息">
          <el-option v-for="item in transports" :key="item.id" :label="item.poe +'('+item.shippingTime+')' + ' → ' +item.poi+'('+item.arrivalTime+')'" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="addEditShow = false ; material = {id:0}">取 消</el-button>
      <el-button type="primary" @click="saveOrUpdatematerial('materialForm')">确 定</el-button>
    </div>
  </el-dialog>
</section>
</template>

<style>
.order {
  height: 60px;
  line-height: 60px;
  text-align: center;
}
.order .line {
  display: inline-block;
  width: 150px;
  border-top: 1px solid #ccc;
}
.order .txt {
  color: #686868;
  vertical-align: middle;
}
</style>

<script>
export default {
  data() {
    return {
      searchKey: "",
      pager: {
        pager: {
          pageCount: 1,
          pageNumber: 1,
          pageSize: 15,
          recordCount: 1
        },
        dataList: []
      },
      addEditShow: false,
      imageUrl: "",
      formLabelWidth: "100px",
      material: {
        id: 0
      },
      manors: [],
      storages: [],
      transports: []
    };
  },
  methods: {
    loadData() {
      this.$api.Material.list(this.pager.pager.pageNumber, result => {
        this.pager = result.pager;
      });
    },
    addmaterial() {
      this.addEditShow = true;
      this.material = {
        id: 0
      };
    },
    handleEdit(index, row) {
      this.material = row;
      this.addEditShow = true;
    },
    handleDelete(index, row) {
      this.$confirm("确认删除该生豆信息?", "删除确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$api.Material.delete(row.id, result => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          window.setTimeout(() => {
            if (this.searchKey) {
              this.doSearch();
            } else {
              this.loadData();
            }
          }, 2000);
        });
      });
    },
    changePage() {
      if (this.searchKey) {
        this.doSearch();
      } else {
        this.loadData();
      }
    },
    doSearch() {
      this.$api.Material.search(
        this.pager.pager.pageNumber,
        this.searchKey,
        result => {
          this.pager = result.pager;
        }
      );
    },
    saveOrUpdatematerial(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          var callback = result => {
            this.changePage();
            this.addEditShow = false;
          };
          this.material.id
            ? this.$api.Material.update(this.material, callback)
            : this.$api.Material.save(this.material, callback);
        } else {
          return false;
        }
      });
    }
  },
  created() {
    this.$api.Material.loadSelects(result => {
      this.loadData();
      this.manors = result.manors;
      this.storages = result.storages;
      this.transports = result.transports;
    });
  }
};
</script>
